/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fullalbum.main;

import fullalbum.GenericServlet;
import fullalbum.dao.FigureDAO;
import fullalbum.bean.UserAlbum;
import fullalbum.dao.UserAlbumDAO;
import fullalbum.vo.RepeatedFigureSuggestedVO;
import fullalbum.vo.UserRepeatedFigureSuggestedVO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leoomoreira
 */
public class MainServlet extends GenericServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        HttpSession session = request.getSession();

        UserAlbumDAO userAlbumDAO = new UserAlbumDAO();

        List<UserAlbum> yourAlbumList = userAlbumDAO.getByUserId(Integer.parseInt(session.getAttribute("userId").toString()));
        request.setAttribute("yourAlbumList", yourAlbumList);

        List<UserAlbum> usersAlbumList = userAlbumDAO.getAllExceptUserId(Integer.parseInt(session.getAttribute("userId").toString()));
        request.setAttribute("usersAlbumList", usersAlbumList);

        FigureDAO figureDAO = new FigureDAO();
        List<RepeatedFigureSuggestedVO> repeatedFigureSuggestedVOs = new ArrayList<RepeatedFigureSuggestedVO>();
        for (UserAlbum ua : yourAlbumList) {
            RepeatedFigureSuggestedVO repeatedFigureSuggestedVO = new RepeatedFigureSuggestedVO();

            repeatedFigureSuggestedVO.setAlbumId(ua.getAlbum().getId());
            repeatedFigureSuggestedVO.setAlbumName(ua.getAlbum().getName());

            List<UserRepeatedFigureSuggestedVO> userRepeatedFigureSuggestedVOs = figureDAO.getRepeatedFiguresSuggested(Integer.parseInt(session.getAttribute("userId").toString()));
            if (userRepeatedFigureSuggestedVOs.size() == 0)
                continue;

            repeatedFigureSuggestedVO.setUserRepeatedFigureSuggestedList(userRepeatedFigureSuggestedVOs);
            repeatedFigureSuggestedVOs.add(repeatedFigureSuggestedVO);
        }
        if (repeatedFigureSuggestedVOs.size() > 0) {
            request.setAttribute("repeatedFigureSuggested", repeatedFigureSuggestedVOs);
        }
       
        RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/main.jsp");
        dispatcher.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
